package com.yami.shop.consign.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yami.shop.bean.enums.ConsignStatus;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.util.PageUtil;
import com.yami.shop.consign.common.dao.ConsignAuditRecordMapper;
import com.yami.shop.consign.common.model.ConsignAuditRecord;
import com.yami.shop.consign.common.service.ConsignAuditRecordService;
import com.yami.shop.sys.common.model.ShopEmployee;
import com.yami.shop.sys.common.model.SysUser;
import com.yami.shop.sys.common.service.ShopEmployeeService;
import com.yami.shop.sys.common.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ConsignAuditRecordServiceImpl extends ServiceImpl<ConsignAuditRecordMapper, ConsignAuditRecord> implements ConsignAuditRecordService {

    @Autowired
    private SysUserService sysUserService;
    
    @Autowired
    private ShopEmployeeService shopEmployeeService;
    
    @Override
    public PageVO<ConsignAuditRecord> getPage(PageDTO pageDTO, Long consignId) {
        PageVO<ConsignAuditRecord> pageVO = PageUtil.doPage(pageDTO, () -> list(new LambdaQueryWrapper<ConsignAuditRecord>()
                .eq(ConsignAuditRecord::getConsignId, consignId).orderByDesc(ConsignAuditRecord::getCreateTime).orderByDesc(ConsignAuditRecord::getId)));
        List<ConsignAuditRecord> list = pageVO.getList();
        list.stream().forEach(a -> {
            if (Objects.nonNull(a.getAuditUser())) {
                if (a.getType() == ConsignStatus.PLAT.getValue()) {
                    SysUser sysUser = sysUserService.getSysUserById(a.getAuditUser());
                    a.setUserName(sysUser.getNickName());
                } else {
                    ShopEmployee shopEmployee = shopEmployeeService.getShopEmployeeById(a.getAuditUser());
                    a.setUserName(shopEmployee.getNickname());
                }
            }
        });
        return pageVO;
    }
    
    
    
}
